//
// Created by Poyyo on 2021/5/25.
//

#include "GraphInMatrixImpl.h"

GraphInMatrixImpl::GraphInMatrixImpl() {
    /*
     * number 图中结点的个数
     * temp 临时存储每个结点的邻接结点个数
     * m 结点关系
     * n 结点关系
     */
    int number, temp, m, n;
    cout << "input the number of the VertexNode:";
    cin >> number;
    /*
     * 初始化容器
     */
    // 初始化结点
    vertexes.resize(number+1);
    for (int i = 1; i <= number; i++) {
        vertexes[i] = {i};
    }
    // 初始化邻接矩阵
    M.resize(number+1);
    for (int i = 1; i <= number; i++) {
        vector<int> tempM(number+1, 0);
        M[i] = {tempM};
    }

    /*
     * 构造邻接矩阵
     */
    cout << "input e:" << endl;
    for (int i = 0; i < number; i++) {
        cin >> m >> temp;
        // 输入每个结点的临界结点
        for (int j = 0; j < temp; j++) {
            cin >> n;
            (M[m])[n] = 1;
        }
    }
}

void GraphInMatrixImpl::ToString() {
    cout << "Vertexes: \n";
    for (int i = 1; i < vertexes.size(); i++) {
        cout << "{" << vertexes[i].getId() << ":" << vertexes[i].getValue() << ":" << vertexes[i].isSelected() << "} ";
    }
    cout << endl;
    cout << "M: " << endl;
    for (int i = 1; i < M.size(); i++) {
        for (int j = 1; j < M[i].size(); j++) {
            cout << M[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

const vector<vector<int>> &GraphInMatrixImpl::getM() const {
    return M;
}

void GraphInMatrixImpl::setM(const vector<vector<int>> &m) {
    M = m;
}

const vector<Vertex> &GraphInMatrixImpl::getVertexes() const {
    return vertexes;
}

void GraphInMatrixImpl::setVertexes(const vector<Vertex> &vertexes) {
    GraphInMatrixImpl::vertexes = vertexes;
}

int GraphInMatrixImpl::GetNext(int id) {
    auto vers = this->M[id];
    for (int i = 1; i <= vers.size(); i++) {
        if (vers[i] != 0) {
            return i;
        }
    }
    return 0;
}



void GraphInMatrixImpl::Visit(int id) {
    cout << id << " ";
}

vector<int> GraphInMatrixImpl::GetLinkedVertexes(int id) {
    vector<int> res;
    for (int i = 1; i < vertexes.size(); i++) {
        if (i == id) {
            continue;
        }
        if (M[id][i] != 0) {
            res.push_back(i);
        }
    }
    return res;
}

void GraphInMatrixImpl::Select(int id) {
    if (id > 0 && id <= vertexes.size()){
        vertexes[id].setSelected(true);
    }
}

